/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author tho.hoang
 */
public class StringItem extends AListItem {

    public StringItem(String id, String text) {
        super(text, null);
        super.id = id;
    }

    public StringItem(String id, String text, Image icon) {
        super(text, icon);
        super.id = id;
    }

    public void paint(int x, int y, Graphics g) {
        paint(x, y, g, false);
    }

    public void paint(int x, int y, Graphics g, boolean validate) {
        if (validate && !isPaint(x, y)) {
            return;
        }
        // pre-calc
        calculateHeight();
        setTextRows(text);

        // paint background
        g.setColor(focus == false ? style.getBgColor() : style.getSelectedBgColor());
        g.fillRect(x, y, width, height);

        // paint text
        int tempX, tempY;
        tempX = x + style.getPadding().getLeft();
        tempY = y + style.getPadding().getTop();
        for (int i = 0; i < textRows.length; i++) {
            font.getFontNormal().drawString(g, textRows[i], tempX, tempY + i * (font.getFontNormal().getHeight() + style.getRowHeight()), Graphics.TOP | Graphics.LEFT);
        }
    }

    public void setText(String text) {
        this.text = text;
        setTextRows(text);
    }

    protected void calculateHeight() {
        int textH = 0, iconH = 0;

        if (icon != null) {
            iconH = icon.getHeight();
        }

        if (textRows == null) {
            setTextRows(text);
        }

        textH = textRows.length * (font.getFontNormal().getHeight() + style.getPadding().getTop()) + style.getPadding().getBottom();

        this.height = iconH > textH ? iconH : textH;
    }

    protected void setTextRows(String text) {
        if (text.length() > 0) {
//            textRows = normalFont.splitWith(text, width);
            textRows = new String[]{text};
        } else {
            textRows = new String[]{""};
        }
    }
}
